/********************************************************
 * Filename: TclMessageDialog.java
 * Author: LMU
 * Program: TUnit
 * Description: Creates a message dialog after the test
 * completes execution.  
 * Revision History:
 * 20060904: Initial Revision.  Added header later.  
 * 20061008: Did a little refactoring.  
 * *****************************************************/

package edu.lmu.cs.tUnit.popup.actions;

import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import edu.lmu.cs.tUnit.plugin.TUnitPlugin;

public class TclMessageDialog extends Dialog {
	private Shell shell;
	final static public int PASS_ICON = 0;
	final static public int FAIL_ICON = 1;
	   
	/**
	 * Create the message dialog at the end of test
	 * @param parent
	 * @param style
	 * @param imageNumber
	 */
	public TclMessageDialog(Shell parent, int style, int imageNumber) {
		super(parent, style);
		shell = new Shell(parent, SWT.DIALOG_TRIM | style);
		shell.setText("TUnit");
		Image image = null;
		
		setupDialog(imageNumber, image);			
		Composite footer = new Composite(shell, SWT.NONE);
		createDialog(footer);
	}

	/**
	 * Shows the dialog
	 * @param footer
	 */
	private void createDialog(Composite footer) {
		// Setup the dialog
		GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWT.FILL;
		data.horizontalSpan = 2;
		footer.setLayoutData(data);

		RowLayout layout = new RowLayout();
		layout.justify = true;
		layout.fill = true;
		footer.setLayout(layout);

		Button ok = new Button(footer, SWT.PUSH);
		ok.setText("    OK    ");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		shell.setDefaultButton(ok);
	}

	/**
	 * Setup the contents of the dialog
	 * @param imageNumber
	 * @param image
	 */
	private void setupDialog(int imageNumber, Image image) {
		URL SAMPLE;
		ImageDescriptor descriptor;
		// Create the dialog icon based on results
		if(imageNumber == PASS_ICON) {
			SAMPLE = TUnitPlugin.getDefault().getBundle().getEntry("/icons/passed.gif");
			descriptor = ImageDescriptor.createFromURL(SAMPLE);
			image = descriptor.createImage();
		}
		
		else if(imageNumber >= FAIL_ICON || imageNumber == -1) {
			SAMPLE = TUnitPlugin.getDefault().getBundle().getEntry("/icons/failed.gif");
			descriptor = ImageDescriptor.createFromURL(SAMPLE);
			image = descriptor.createImage();
		}
		
		//shell.setImage(image);
		shell.setLayout(new GridLayout(2, false));
	       
		new Label(shell, SWT.CENTER).setImage(image);
	       
		// Create message for the user
		if(imageNumber == PASS_ICON) 
			new Label(shell, SWT.CENTER).setText("TUnit Test Passed with no failures!");
		else if(imageNumber == FAIL_ICON)
			new Label(shell, SWT.CENTER).setText("TUnit Test Failed with 1 failure.");
		else if(imageNumber > FAIL_ICON)
			new Label(shell, SWT.CENTER).setText("TUnit Test Failed with " + imageNumber + " failures.");
		else new Label(shell, SWT.CENTER).setText("File not found error received.");
	}
	   	   	   
	public void open() {
		shell.pack();
		shell.open();
		shell.layout();

		while (!shell.isDisposed())
			if (!shell.getDisplay().readAndDispatch()) 
				shell.getDisplay().sleep();
	}
}
